package com.ct.boot.launch.controller;

import com.ct.boot.launch.AjaxResponse;
import com.ct.boot.launch.dao.testdb.Article;
import com.ct.boot.launch.model.ArticleVO;
import com.ct.boot.launch.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: 1
 * @Date: 2020/6/12 08:49
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/rest")
public class ArticleController {
//    @Resource
//    ArticleService articleService;
    @Autowired
    private ArticleService articleService;
    @RequestMapping( value = "/articles/{id}",method = RequestMethod.GET)
    public AjaxResponse getArticle(@PathVariable("id") Long id){
        System.out.println(id);
        ArticleVO article = articleService.getArticle(id);
        return AjaxResponse.success(article,"查询成功");
    }
    @RequestMapping( value = "/articles/",method = RequestMethod.GET)
    public AjaxResponse getAllArticle(){
        List<ArticleVO> all = articleService.getAll();
        return AjaxResponse.success(all,"全部数据");
    }

    @RequestMapping( value = "/articles/",method = RequestMethod.PUT)
    public AjaxResponse updateArticle(@RequestBody ArticleVO article) throws Exception {

        articleService.updateArticle(article);
        log.info("updateArticle"+article);
        return AjaxResponse.success(article,"更新成功");
    }

    @RequestMapping( value = "/articles/add",method = RequestMethod.POST)
    public AjaxResponse deleteArticle(@RequestBody ArticleVO article) {
        String s = articleService.saveArticle(article);
        return AjaxResponse.success(article,s);
    }


}
